home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Constructor Exceptions
- Date: 7 Jan 1996 22:35:08 GMT
- Organization: self-employed
- Message-ID: <4cphqs$hlc@news.bridge.net>
- References: <4bud9g$pv5@oxy.rust.net> <4cbhcl$kst@dawn.mmm.com> <4ce0da$513@oxy.rust.net>
- NNTP-Posting-Host: ppp-mia1-39.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
- Paul;
-
- When an exception is thrown from an object's constructor, that
- object's memory should be cleaned up, according to the draft
- standard. If it's a stack object, then of course this will
- happen as the stack frame is erased. If it's a heap object, the
- 'new' keyword is supposed to take care of the problem.
-
- Memory and resources which the constructor has already allocated
- should be freed, of course, but this is the programmer's
- responsibility. There are two means for doing so; first, wrapping
- every resource in a class so its destructor will free it;
- second, catching all exceptions at the end of the constructor
- with a catch(...) block, freeing resources there, and rethrowing.
-
- The use of two-phase initialisation is not something I would
- recommend. Templates will make it undesirable. How can you tell
- a template that your particular class needs init() called
- after the constructor?
-
- However, note that many classes have a "null value", which is
- created by the default constructor, and typically an object
- with the null value will own no resources. This is not
- two-phase construction, although it looks similar.
-
- Can you tell me offhand whether 'new' in VC2.1 is actually freeing
- the heap memory of objects which throw?
-
-
- David Byrden
-
- || My opinions ARE those of my employer ||
-
-
-